package com.itheima.oldTime;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName: DateTimeUtil
 * @Description:
 * @author: Yrd
 * @date: 2021-10-24 12:20
 */
public class DateTimeUtil {

    // jdk建议将 ThreadLocal 定义为 private static ，这样就不会有弱引用，内存泄漏的问题了
    private static final ThreadLocal<Map<String,SimpleDateFormat>> sdfMapThreadLocal = new ThreadLocal<Map<String, SimpleDateFormat>>(){

        //  ThreadLocal没有被当前线程赋值时或当前线程刚调用remove方法后调用get方法，返回此方法值
        @Override
        protected Map<String, SimpleDateFormat> initialValue() {
            Map formatMap = new LinkedHashMap();
            formatMap.put("yyyy-MM-dd",new SimpleDateFormat("yyyy-MM-dd"));
            formatMap.put("HH:mm:ss",new SimpleDateFormat("HH:mm:ss"));
            formatMap.put("yyyy-MM-dd HH:mm:ss",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            return formatMap;
        }
    };

    // jdk建议将 ThreadLocal 定义为 private static ，这样就不会有弱引用，内存泄漏的问题了
    private static final ThreadLocal<Calendar> calendarThreadLocal = new ThreadLocal<Calendar>(){
        //  ThreadLocal没有被当前线程赋值时或当前线程刚调用remove方法后调用get方法，返回此方法值
        @Override
        protected Calendar initialValue() {
            return Calendar.getInstance();
        }
    };


    //无参构造函数
    public DateTimeUtil() {
    }

    public static SimpleDateFormat getSimpleDateFormat(String pattern){
        if(sdfMapThreadLocal.get().containsKey(pattern)){
            return sdfMapThreadLocal.get().get(pattern);
        }else {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
            sdfMapThreadLocal.get().put(pattern,simpleDateFormat);
            return simpleDateFormat;
        }
    }

    public static Calendar getCalendar(){
        Calendar calendar = calendarThreadLocal.get();
        //初始化日期时间
        calendar.setTimeInMillis(System.currentTimeMillis());
        return calendar;
    }

    public static Calendar getCalendar(Date date){
        Calendar calendar = calendarThreadLocal.get();
        //初始化日期时间
        calendar.setTimeInMillis(date.getTime());
        return calendar;
    }

    public static Calendar getCalendar(long date){
        Calendar calendar = calendarThreadLocal.get();
        //初始化日期时间
        calendar.setTimeInMillis(date);
        return calendar;
    }

    public static Date getNow(){
        return new Date();
    }

    public static String getDateStr(){
        return getDateTimeStr("yyyy-MM-dd");
    }

    public static String getDateTimeStr(){
        return getDateTimeStr("yyyy-MM-dd HH:mm:ss");
    }

    public static String getDateTimeStr(String pattern){
        return getDateTimeStr(new Date(),pattern);
    }

    public static String getDateTimeStr(Date date,String pattern){
        if(null == pattern || "".equals(pattern)){
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        return getSimpleDateFormat(pattern).format(date);
    }

    public static int getCurrentYear(){
        return getCalendar().get(Calendar.YEAR);
    }

    public static int getCurrentMonth(){
        return getCalendar().get(Calendar.MONTH);
    }

    private static Date add(Date date,int amount,int field){
        Calendar calendar = calendarThreadLocal.get();
        calendar.setTime(date);
        calendar.add(field,amount);
        return calendar.getTime();
    }

    public static Date addDays(Date date,int days){
        return add(date,days,Calendar.DAY_OF_MONTH);
    }

    /**
     *
     * @Title : parse
     * @Description : 字符串转日期

     * @param dateStr
     * @param pattern
     * @return java.util.Date
     *
     * @user : Yrd
     * @date : 2021-10-24 14:02
     */
    public static Date parseStrToDate(String dateStr,String pattern){
        Date date =null;
        if(null == pattern || "".equals(pattern)){
            pattern = "yyyy-MM-dd";
        }
        try {
            date = getSimpleDateFormat(pattern).parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     *
     * @Title : getMonthLastDay
     * @Description : 获取月末日期

     * @param date
     * @return java.util.Date
     *
     * @user : Yrd
     * @date : 2021-10-24 14:05
     */
    public static Date getMonthLastDay(Date date){
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        //将时间设置为下个月的一号，然后减去一天，就是月末日期了
        calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH)+1,1);
        calendar.add(Calendar.DAY_OF_MONTH,-1);
        return calendar.getTime();
    }

    /**
     *
     * @Title : isDate
     * @Description : 校验日期是否合法 格式：yyyymmdd  如：20201201 合法 20201238不合法

     * @param date
     * @return boolean
     *
     * @user : Yrd
     * @date : 2021-10-24 14:21
     */
    public static boolean isDate(String date){
        Pattern pattern = Pattern.compile("^[1-9]\\d{3}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])$");
        Matcher matcher = pattern.matcher(date);
        if(matcher.matches()){
            return true;
        }else {
            return false;
        }
    }

















}
